package com.xinqi.modules.sys.dict.controller.app;

import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.modules.sys.dict.dto.req.DictDataCreateDTO;
import com.xinqi.modules.sys.dict.dto.req.DictDataQueryDTO;
import com.xinqi.modules.sys.dict.dto.req.DictDataUpdateDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictDataResultDTO;
import com.xinqi.modules.sys.dict.service.DictDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 字典数据表APP控制层
 *
 * @author: linkai
 * @date: 2023/05/21
 */
@Tag(name = "App-字典数据表", description = "字典数据表App接口")
@RestController
@RequestMapping("/app/dict-data")
@RequiredArgsConstructor
public class DictDataAppController {
    private final DictDataService dictDataService;

    /**
     * 字典数据表-分页
     */
    @Operation(summary = "字典数据表-分页")
    @GetMapping("/page")
    public R<PageInfo<DictDataResultDTO>> page(@ParameterObject DictDataQueryDTO dto) {
        PageInfo<DictDataResultDTO> page = dictDataService.page(dto, dto);
        return R.data(page);
    }

    /**
     * 字典数据表-详情
     */
    @Operation(summary = "字典数据表-详情")
    @GetMapping("/{id}")
    public R<DictDataResultDTO> findById(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        DictDataResultDTO dto = dictDataService.findById(id);
        return R.data(dto);
    }

    /**
     * 新增字典数据表
     */
    @Operation(summary = "新增字典数据表")
    @PostMapping
    public R<Long> insert(@RequestBody DictDataCreateDTO dto) {
        return R.data(dictDataService.insert(dto));
    }

    /**
     * 修改字典数据表
     */
    @Operation(summary = "修改字典数据表")
    @PutMapping("/{id}")
    public R<Boolean> update(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id,
                             @RequestBody DictDataUpdateDTO dto) {
        dto.setId(id);
        return R.data(dictDataService.updateById(dto));
    }

    /**
     * 删除字典数据表
     */
    @Operation(summary = "删除字典数据表")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        return R.data(dictDataService.deleteById(id));
    }

    /**
     * 根据字典类型查询字典数据信息
     *
     * @param dto 字典参数
     */
    @GetMapping(value = "/tree")
    public R<List<DictDataResultDTO>> treeList(DictDataQueryDTO dto) {
        return R.ok(dictDataService.treeList(dto));
    }
}
